import 'dart:convert';

import 'package:dali_ng_pera/manager/dnp_plugin_manager.dart';
import 'package:dali_ng_pera/net/dnp_net_manager.dart';
import 'package:dali_ng_pera/page/calculator/dnp_calculator_page.dart';
import 'package:dali_ng_pera/page/credit/dnp_credit_page.dart';
import 'package:dali_ng_pera/page/widget/dnp_marquee_widget.dart';
import 'package:dio/dio.dart' as dio;
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../../../global/dnp_global_manager.dart';
import '../../frequentlyQuestions/dnp_frequency_question_page.dart';
import '../../register/dnp_register_page.dart';
import '../model/dnp_home_data_model.dart';
import '../model/dnp_home_rolled_data_model.dart';
import 'dnp_home_heard_mixin.dart';

/// 大卡位UI mixin
mixin DnpHomeBigCardMixin implements DnpHomeHeardMixin {
  DnpSyllable? _syllable;
  DnpCitizen? _citizen;
  List<String> marquee = [];

  void initBigCardData(DnpHomeDataModel model) {
    _syllable = getSyllable(model,"ob");
    _citizen = getCitizen(model,"ob");
    marquee = model.mumbled??[];
  }

  Widget buildBigCard(BuildContext context,DnpHomeDataModel model) {
    initBigCardData(model);
    return CustomScrollView(
      physics: const BouncingScrollPhysics(),
      slivers: [
        SliverToBoxAdapter(
          child: const SizedBox(height: 124),
        ),
        SliverToBoxAdapter(child: _buildBigCardTop()),
        SliverToBoxAdapter(child: const SizedBox(height: 14)),
        SliverToBoxAdapter(child: _buildBigCardHorn()),
        SliverToBoxAdapter(child: const SizedBox(height: 20)),
        SliverToBoxAdapter(child: _buildBigCardCenter(context)),
        SliverToBoxAdapter(child: const SizedBox(height: 14)),
        SliverToBoxAdapter(child: _buildBigCardLoanKnowledgeCredit(context)),
        SliverToBoxAdapter(child: const SizedBox(height: 16)),
        SliverToBoxAdapter(child: _buildBigCardOurStrengths()),
        SliverToBoxAdapter(child: const SizedBox(height: 14)),
      ],
    );
  }

  Widget _buildBigCardTop() {
    return Image.asset(
      "assets/images/ic_big_top_bg.webp",
      fit: BoxFit.fill,
      width: double.infinity,
      height: 124,
    );
  }

  Widget _buildBigCardHorn() {
    return Container(
      width: double.infinity,
      padding: EdgeInsets.symmetric(horizontal: 12, vertical: 6),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(20),
        border: Border.all(color: Color(0xFF1D4AC8), width: 1),
      ),
      child: Row(
        children: [
          Image.asset(
            "assets/images/ic_small_horn_icon.png",
            width: 20,
            height: 16,
          ),
          SizedBox(width: 6),
          Container(
            width: 1,
            height: 16,
            color: Colors.grey,
          ),
          SizedBox(width: 6),
          Expanded(
            child: DnpMarqueeWidget(
              messages: marquee,
              duration: 3,
              animationDuration: 500,
              height: 16,
              textStyle: const TextStyle(
                fontSize: 12,
                color: Colors.black87,
                height: 1.0,
              ),
              alignment: Alignment.centerLeft,
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildBigCardCenter(BuildContext context) {
    return GestureDetector(
      behavior: HitTestBehavior.translucent,
      onTap: (){
        bool isRegister = DnpGlobalManager().isRegister;

        if (isRegister == false) {
          Navigator.of(context).push(MaterialPageRoute(builder: (_) => DnpRegisterPage()));
          return;
        }
        DnpPluginManager().showLoading();
        DnpNetManager()
            .post("/haseye/rolled",
            data: dio.FormData.fromMap({'already': "${_citizen?.staring}"}))
            .then((value) {
          DnpPluginManager().hideLoading();
          if (value.success == true) {
            DnpHomeRolledDataModel model =
            DnpHomeRolledDataModel.fromJson(value.close);
            handleJump(model.belated??"",_citizen?.tossing ?? "");
          } else {
            DnpPluginManager().showToast(value.kurios ?? "");
          }
        });
      },
      child: Container(
          width: double.infinity,
          height: 247,
          decoration: BoxDecoration(
            image: DecorationImage(
                image: AssetImage("assets/images/ic_small_center_bg.webp"),
                fit: BoxFit.fill),
          ),
          child: SingleChildScrollView(
            child: Column(children: [
              SizedBox(height: 19),
              Text(
                _citizen?.later ?? "",
                style: TextStyle(fontSize: 14, color: Color(0xFFC1C1C1)),
              ),
              SizedBox(height: 10),
              _buildBigCardCenterMoney(),
              SizedBox(height: 12),
              _buildBigCardCenterButton(),
              SizedBox(height: 20),
              _buildBigCardCenterApply(context),
            ]),
          )),
    );
  }

  Widget _buildBigCardCenterMoney() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        Text(
          _citizen?.week ?? "",
          style: TextStyle(
              fontSize: 48,
              color: Color(0xFF252630),
              fontWeight: FontWeight.w900),
        ),
      ],
    );
  }

  Widget _buildBigCardCenterButton() {
    return Row(
      children: [
        SizedBox(width: 20),
        _buildBigCardCenterButtonItem(
            _citizen?.three ?? "", _citizen?.hours ?? ""),
        SizedBox(width: 12),
        _buildBigCardCenterButtonItem(
            _citizen?.stayed ?? "", _citizen?.custom ?? ""),
        SizedBox(width: 20),
      ],
    );
  }

  Widget _buildBigCardCenterButtonItem(String title, String content) {
    return Expanded(
        child: Container(
      height: 56,
      width: double.infinity,
      decoration: BoxDecoration(
        color: Colors.white,
        border: Border.all(color: Color(0xFF0193FE), width: 1),
        borderRadius: BorderRadius.circular(56),
      ),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Text(
            title,
            style: TextStyle(fontSize: 14, color: Color(0xFF252630)),
          ),
          Text(
            content,
            style: TextStyle(
                fontSize: 16,
                color: Color(0xFF252630),
                fontWeight: FontWeight.w600),
          ),
        ],
      ),
    ));
  }

  Widget _buildBigCardCenterApply(BuildContext context) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        Text(
          _citizen?.earnestly ?? "",
          style: TextStyle(
              fontWeight: FontWeight.w900, fontSize: 22, color: Colors.white),
        ),
        SizedBox(width: 10),
        Image.asset(
          "assets/images/ic_small_center_arrow.png",
          width: 24,
          height: 24,
        ),
      ],
    );
  }

  Widget _buildBigCardLoanKnowledgeCredit(BuildContext context) {
    return SizedBox(
      height: 184,
      width: double.infinity,
      child: Row(
        children: [
          Expanded(
              child: Column(
            children: [
              Expanded(
                  child: GestureDetector(
                    onTap: (){
                      bool isRegister = DnpGlobalManager().isRegister;
                      if (isRegister == false) {
                        Navigator.of(DnpGlobalManager().ctx!).push(MaterialPageRoute(builder: (_) => DnpRegisterPage()));
                        return;
                      }
                      Get.to(DnpCalculatorPage());
                    },
                    child: Image.asset(
                                    "assets/images/ic_big_loan_calculator_bg.webp",
                                    width: double.infinity,
                                    height: double.infinity,
                                  ),
                  )),
              SizedBox(height: 14),
              Expanded(
                  child: GestureDetector(
                    onTap: (){
                      bool isRegister = DnpGlobalManager().isRegister;
                      if (isRegister == false) {
                        Navigator.of(DnpGlobalManager().ctx!).push(MaterialPageRoute(builder: (_) => DnpRegisterPage()));
                        return;
                      }
                      Get.to(DnpFrequencyQuestionPage());
                    },
                    child: Image.asset(
                                    "assets/images/ic_big_knowledge_bg.webp",
                                    width: double.infinity,
                                    height: double.infinity,
                                  ),
                  )),
            ],
          )),
          SizedBox(width: 14),
          Expanded(
              child: GestureDetector(
                onTap: (){
                  bool isRegister = DnpGlobalManager().isRegister;

                  if (isRegister == false) {
                    Navigator.of(context).push(MaterialPageRoute(builder: (_) => DnpRegisterPage()));
                    return;
                  }
                  Get.to(DnpCreditPage());
                },
                child: Image.asset(
                            "assets/images/ic_big_one_click_credit_bg.webp",
                            width: double.infinity,
                            height: double.infinity,
                          ),
              )),
        ],
      ),
    );
  }

  Widget _buildBigCardOurStrengths() {
    return SizedBox(
        width: double.infinity,
        height: 162,
        child: Image.asset(
          "assets/images/ic_bg_bottom_bg.webp",
          width: double.infinity,
          height: double.infinity,
        ));
  }

  @override
  String get heard => "ob";
}
